package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 * Servlet implementation class GetTP
 */
//@WebServlet(description = "取得測試計畫案要Update", urlPatterns = { "/GetTP" })
public class GetTP extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetTP() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Widgets wid = new Widgets();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String strSQL = "SELECT t1.userID,t2.nickName FROM activiti.tms_tpowner t1, tms_personnel t2 WHERE t1.userID = t2.userID AND t1.tpid=?";
		String returnResult = "";
		int tpid = Integer.parseInt(request.getParameter("tpid"));
		ArrayList<TestPlan> tps = new ArrayList<TestPlan>();
		ArrayList<User> users = new ArrayList<User>();
		TestPlan tp = new TestPlan();
		try {
			conn = wid.getConn(true);
			ps = conn.prepareStatement(strSQL);
			ps.setInt(1, tpid);
			rs = ps.executeQuery();
			while (rs.next()) {
				User user = new User();
				if (!"null".equalsIgnoreCase(rs.getString("userID"))) {
					user.setUserID(rs.getString("userID"));
					user.setNickName(rs.getString("nickName"));
					users.add(user);
				}
			}

			strSQL = "SELECT tpName, comment, createdDate, lastUpdatedDate FROM activiti.tms_tp WHERE tpid=?";
			ps = conn.prepareStatement(strSQL);
			ps.setInt(1, tpid);
			rs = ps.executeQuery();
			rs.next();
			System.out.println(strSQL);

			tp.setTpName(rs.getString("tpName"));
			tp.setComment(rs.getString("comment"));

			DateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

			tp.setScreateDate(dateFormat.format(rs.getTimestamp("createdDate")));
			tp.setSlastUpdateDate(dateFormat.format(rs
					.getTimestamp("lastUpdatedDate")));
			tp.setTpOwner(users);
			tps.add(tp);
		} catch (SQLException e) {
			returnResult = "{\"errorMsg\":\"" + wid.keepErrorMes(conn, ps, e,"", true) + "\"}";
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps != null) {ps.close();}
				if (conn != null) {conn.close();}
			} catch (SQLException e) {
				returnResult = "{\"errorMsg\":\"" + e.toString() + "\"}";
			}
		}

		returnResult = new Gson().toJson(tps);
		returnResult = returnResult.substring(1, returnResult.length());
		returnResult = "{\"sqlData\":[" + returnResult + ",\"strSQL\":\""
				+ strSQL + "****" + tpid + "\",\"size\":\"" + tps.size()
				+ "\"}";

		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(returnResult);
	}

}
